package com.doge.walknovel.business.novel.chapter

import androidx.lifecycle.LiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.core.base.adapters.toLiveData

class NovelChapterViewModel constructor(private val repository: NovelChapterRepository) :
    ViewModel() {
    val loading: LiveData<LoadStatus> = repository.loading
    val dataList = repository.dataSource().toLiveData(
        cellId = { it.id },
        requestMore = { repository.request(false) })

    fun request() {
        repository.request(true)
    }
}


class NovelChapterViewModelFactory(private val requestId: Int) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        val repository = NovelChapterRepository(requestId)
        if (modelClass.isAssignableFrom(NovelChapterViewModel::class.java)) {
            return NovelChapterViewModel(repository) as T
        }
        //noinspection unchecked
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

